package com.sf.fy.uav.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sf.fy.uav.common.enums.PricingRuleRangeTypeEnum;
import com.sf.fy.uav.common.enums.PricingRuleTypeEnum;
import com.sf.fy.uav.common.request.pricing.PricingAddRequest;
import com.sf.fy.uav.common.utils.BeanUtil;
import com.sf.fy.uav.order.entity.PricingRule;
import com.sf.fy.uav.order.mapper.PricingRuleMapper;
import com.sf.fy.uav.order.service.IPricingRuleService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 定价规则表 服务实现类
 * </p>
 *
 * @author LTQ
 * @since 2023-11-14
 */
@Service
public class PricingRuleServiceImpl extends ServiceImpl<PricingRuleMapper, PricingRule> implements IPricingRuleService {
    @Autowired
    private PricingRuleMapper pricingRuleMapper;
    private final Integer CONT_NUM = 0;
    private final String BASE_NUM_CHECK = "^[0-9]+(\\.[0-9]{1,1})?$";

    private final String ACCOUNT_NUM_CHECK = "^[0-9]+(\\.[0-9]{1,2})?$";

    @Override
    public void add(PricingAddRequest pricingAddRequest) {
        Date date = new Date();
        if (!CollectionUtils.isEmpty(pricingAddRequest.getPricingRuleRangeArray())) {
            for (PricingAddRequest.PricingRuleRange pricingRuleRange : pricingAddRequest.getPricingRuleRangeArray()) {

                if (!org.springframework.util.ObjectUtils.isEmpty(pricingRuleRange.getType()) && (PricingRuleTypeEnum.WEIGHT.getType().equals(pricingRuleRange.getType()) || PricingRuleTypeEnum.DISTANCE.getType().equals(pricingRuleRange.getType()))) {
                    if (ObjectUtils.isEmpty(pricingRuleRange.getBaseNum()) || pricingRuleRange.getBaseNum().compareTo(BigDecimal.ZERO) == CONT_NUM || pricingRuleRange.getBaseNum().compareTo(BigDecimal.ZERO) < CONT_NUM) {
                        throw new RuntimeException("基础重量或基础距离不能为空或为0以及负数");
                    }
                    if (!pricingRuleRange.getBaseNum().toString().matches(BASE_NUM_CHECK)) {
                        throw new RuntimeException("基础重量或基础距离仅支持输入正数或一位小数");
                    }
                    if (!CollectionUtils.isEmpty(pricingRuleRange.getWeightOrDistanceList())) {
                        for (PricingAddRequest.PricingRuleRange.WeightOrDistance weightOrDistance : pricingRuleRange.getWeightOrDistanceList()) {
                            if (ObjectUtils.isEmpty(weightOrDistance.getRangeTypes())) {
                                throw new RuntimeException("区间或大于类型未传");
                            }
                            if (ObjectUtils.isEmpty(weightOrDistance.getStartNum()) || weightOrDistance.getStartNum().compareTo(BigDecimal.ZERO) == CONT_NUM || weightOrDistance.getStartNum().compareTo(BigDecimal.ZERO) < CONT_NUM) {
                                throw new RuntimeException("起始重量或起始距离不能为空或为0以及负数");
                            }
                            if (!weightOrDistance.getStartNum().toString().matches(BASE_NUM_CHECK)) {
                                throw new RuntimeException("基础重量或基础距离仅支持输入正数或一位小数");
                            }
                            if (PricingRuleRangeTypeEnum.BETWEEN.getType().equals(weightOrDistance.getRangeTypes())) {
                                if (ObjectUtils.isEmpty(weightOrDistance.getEndNum()) || weightOrDistance.getEndNum().compareTo(BigDecimal.ZERO) == CONT_NUM || weightOrDistance.getEndNum().compareTo(BigDecimal.ZERO) < CONT_NUM) {
                                    throw new RuntimeException("结束值不能为空或为0以及负数");
                                }
                                if (!weightOrDistance.getEndNum().toString().matches(BASE_NUM_CHECK)) {
                                    throw new RuntimeException("基础重量或基础距离仅支持输入正数或一位小数");
                                }
                                if (weightOrDistance.getStartNum().compareTo(weightOrDistance.getEndNum()) > CONT_NUM) {
                                    throw new RuntimeException("起始值不能大于结束值");
                                }
                            }

                            if (ObjectUtils.isEmpty(weightOrDistance.getMarkupPrice()) || weightOrDistance.getMarkupPrice().compareTo(BigDecimal.ZERO) == CONT_NUM || weightOrDistance.getMarkupPrice().compareTo(BigDecimal.ZERO) < CONT_NUM) {
                                throw new RuntimeException("加价金额不能为空或为0以及负数");
                            }
                            if (!weightOrDistance.getMarkupPrice().toString().matches(ACCOUNT_NUM_CHECK)) {
                                throw new RuntimeException("加价金额仅支持输入正数或两位小数");
                            }
                            if(ObjectUtils.isEmpty(weightOrDistance.getSort())){
                                throw new RuntimeException("排序字段不能为空");
                            }
                        }
                    }
                }
            }
            int num = 0;
            List<PricingRule> pricingRules = BeanUtil.copyProperties(pricingAddRequest.getPricingRuleRangeArray(), PricingRule.class);
            for (int i = 0; i < pricingRules.size(); i++) {
                if(ObjectUtils.isEmpty(pricingRules.get(i).getType())){
                    num++;
                    continue;
                }
                pricingRules.get(i).setCreateTime(date);
                pricingRules.get(i).setUpdateTime(date);
                pricingRules.get(i).setPricingId(pricingAddRequest.getPricingId());
                pricingRules.get(i).setId(pricingAddRequest.getPricingRuleIds().get(i));
            }
            if(!CollectionUtils.isEmpty(pricingRules)&&num==0){
                pricingRuleMapper.insertBatchSomeColumn(pricingRules);
            }
        }
    }
}

